package com.terrapages.wmsc4j.layer.resolution;

import java.awt.Dimension;

import com.terrapages.wmsc4j.bean.BBox;

/**
 * Represents a tilecache resolution.
 * Defined as the number of degrees per pixel in a map image
 * 
 * Essentially just a wrapper for {@link Double} with one key difference.
 * The compareTo function is the inverse of the Doubles compareTo function.
 * Ie Higher Resolutions (zoomed out) come before lower resolutions (zoomed in).
 * 
 * @author mvivian
 * 
 * @date 21 May 2007
 */
public class Resolution implements Comparable<Resolution>{
	private Double value;

	/**
	 * Creates a resolution
	 * @return
	 */
	public Resolution(final double value) {
		super();
		this.setValue(value);
	}
	
	/**
	 * Caluculates and creates the resolution from a boundingbox and an image size
	 * @param boundingBox
	 * @param imageSize
	 */
	public Resolution(final BBox boundingBox, final Dimension imageSize)
	{
		this(Math.max(
				boundingBox.getWidth() / imageSize.getWidth(), 
				boundingBox.getHeight() / imageSize.getHeight()
		));
	}
	
	/**
	 * Gets the double value of the resolution
	 * @return
	 */
	public final double getValue() {
		return value;
	}

	/**
	 * Sets the double value of the resolution
	 * @throws IllegalArgumentException if the resolution is not positive
	 */
	public final void setValue(final double value) throws IllegalArgumentException{
		if(value <= 0.0)
		{
			throw new IllegalArgumentException("Resolution must be positive, argument was " + value);
		}
		this.value = value;
	}

	/**
	 * Compares this Resolution to another. Higher Resolutions are before Lower Resolutions
	 */
	public int compareTo(Resolution o) {
		// TODO Auto-generated method stub
		return -1* value.compareTo(o.getValue());
	}
	
}
